package com.woniuxy.joylife.manager.controller;

import com.woniuxy.common.constant.ManagerConstant;
import com.woniuxy.common.utils.KeyUtil;
import com.woniuxy.common.validate.AddGroup;
import com.woniuxy.common.validate.UpdateGroup;
import com.woniuxy.joylife.manager.entity.DepartmentEntity;
import com.woniuxy.joylife.manager.service.DepartmentService;
import com.woniuxy.joylife.manager.utils.PageUtils;
import com.woniuxy.joylife.manager.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;



/**
 * 部门信息
 *
 * @author renxiang
 * @email 981405195@qq.com
 * @date 2020-05-13 02:47:54
 */
@RestController
@RequestMapping("manager/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("manager:department:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = departmentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{depId}")
    //@RequiresPermissions("manager:department:info")
    public R info(@PathVariable("depId") String depId){
		DepartmentEntity department = departmentService.getById(depId);

        return R.ok().put("department", department);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("manager:department:save")
    public R save(@Validated(AddGroup.class) @RequestBody DepartmentEntity department) throws Exception {
        department.setDepId(KeyUtil.generateKey());
        department.setState(ManagerConstant.UNDELETED);
        department.setCreatetime(new Date());
		if(!departmentService.saveDepartmentEntity(department)){
		    throw new Exception("添加失败, 部门信息重复");
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("manager:department:update")
    public R update(@Validated(UpdateGroup.class)@RequestBody DepartmentEntity department){
        department.setUpdatetime(new Date());
		departmentService.updateById(department);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("manager:department:delete")
    public R delete(@RequestBody String[] depIds){
		departmentService.removeByIds(Arrays.asList(depIds));

        return R.ok();
    }

}
